<?php
	
	require_once 'NavigationView.php';

	class ForumView {
	
		private $aDate = "date";
		private $answer = "answer";
		private $answerSubmit = "answerSubmit";
		private $del = "delete";
		private $delAnswer = "deleteAnswer";
		private $detail = "detail";
		private $editTopic = "editTopic";
		private $editSubmit ="editSubmit";
		private $id = "id";
		private $inputTopic = "topic";
		private $list = "list";
		private $newAnswer = "newAnswer";
		private $newButton = "newButton";
		private $submit = "submit";
		private $text = "text";
		private $topic = "topic";
		private $userName = "username";
		
		public function CreateNew() {
			
			return "
				<form method='post' id='questionId'>
					<p><input type='submit' value='Nytt inlägg' name='$this->newButton' class='buttonForum'></p>
				</form>";
		}
				
		public function NewTopic() {
			
			return "
				<form method='post' id='newTopic'>
					<p>Rubrik:</p>
					<p><input type='text' name='$this->inputTopic' class='inputTopic' /></p>
					<p><textarea name='$this->detail'></textarea></p>
					<p><input type='submit' value='Skapa inlägg' name='$this->submit' class='buttonForum'></p>
				</form>";
		}
		
		public function CreateNewAnswer() {
			
			return "		
				<form method='post' id='answerId'>
					<p><input type='submit' value='Svara på inlägg' name='$this->newAnswer' class='buttonForum'></p>
				</form>";
		}
		
		public function NewAnswer() {
			
			return "
				<form method='post' id='newAnswer'>
					<p><textarea name='$this->answer' cols='50' rows='3'></textarea></p>
					<p><input type='submit' value='Svara' name='$this->answerSubmit' class='button'></p>
				</form>";	
		}
		
		public function UpdateTopic() {
		
			return "
				<form method='post' id='newAnswer'>
					<p><textarea name='$this->editTopic' cols='50' rows='3'></textarea></p>
					<p><input type='submit' value='Spara' name='$this->editSubmit' class='button'></p>
				</form>";	
		}
		
		public function Deleted() {
			
			return "<div id='successDelete'>Inlägget är borttaget.</div>";		
		}
		
		public function WrongUser() {
			
			return "<div id='errorForum'>Du kan bara redigera/radera det du själv skapat.</div>";
		}
		
		public function WrongFormat() {
			
			return "<div id='errorForum'>Fel inmatning.</div>";
		}
		
		//Skriver ut alla ämnen i en tabell.
		public function ViewAllTopics($topics) {
			
			$table = "
					<table>
						<tr>
							<td class='header'>Ämne</td>
							<td class='header'>Skapad av:</td>
							<td class='header'>Datum</td>
							<td class='header'>*</td>
						</tr>";
					
					if ($topics == null) {
						$table .= "
							<tr>
								<td>Finns inga inlägg.</td>
							</tr>";
					}
					else {
						
					foreach ($topics as $key => $value) {
						$linkToTopic = $this->SetTopicLink($key);
						$delete = $this->SetDeleteLink($key);
						$edit = $this->SetEditLink($key);
						
						$table .= "
							<tr>
								<td>".$value[$this->topic]."</td>
								<td>".$value[$this->userName]."</td>
								<td>".$value[$this->aDate]."</td>
								<td>
									<a href='".$linkToTopic."' class='tableLink'>Visa</a>/
									<a href='".$edit."' class='tableLink'>Redigera</a>/
									<a href='".$delete."' class='tableLink'>Ta bort</a>
								</td>
							</tr>";
						}
					}
					
			$table .= "</table>";
			
			return $table;
					
		}
		
		//Skriver ut alla svar för ett visst ämne.
		public function ViewAnswers($details, $answers) {
			
			$result = "
				<div id='threads'>
				<a href='?".$this->list."' class='tableLink'>Tillbaka till listan</a>";
			
			foreach ($details as $key => $value) {
						
				$result .= "
					<div id='question'>
						<div id='questionHeader'>
							<h3>".$value[$this->topic]."</h3>
						</div>
						<div id='questionMain'>
							<p>".$value[$this->text]."</p>
						</div>
						<div id='questionBottom'>
							<p>Skrivet av: ".$value[$this->userName].",
							".$value[$this->aDate]."</p>
						</div>
					</div>";
			}
			
			if ($answers == null) {
				$result .= "<p class='noAnswers'>Finns inga svar.</p>";	
			}
			else {
				foreach ($answers as $key => $value) {
						
					$delete = $this->SetDeleteAnswerLink($value[$this->id]);
					$result .= "
						<div id='answer'>
							<div id='answerMain'>
								<a href='".$delete."' class='delImg'><img src='./css/x1.png'></a>
								<p>".$value[$this->text]."</p>
							</div>
							<div id='answerBottom'>
								<p>Skrivet av: ".$value[$this->userName].",
								".$value[$this->aDate]."</p>
							</div>
						</div>";
				}
			}
			
			$result .= "</div>";
			
			return $result;		
		}
		
		//Returnera från formulär.
		public function GetTopic() {
			if (isset($_POST[$this->inputTopic]) == true) {
				return $_POST[$this->inputTopic];
			}	
			return false;				
		}
		
		public function GetDetail() {
			if (isset($_POST[$this->detail]) == true) {
				return $_POST[$this->detail];
			}	
			return false;				
		}
		
		public function GetAnswer() {
			if (isset($_POST[$this->answer]) == true) {
				return $_POST[$this->answer];
			}	
			return false;				
		}
		
		public function GetEditTopic() {
			if (isset($_POST[$this->editTopic]) == true) {
				return $_POST[$this->editTopic];
			}	
			return false;				
		}
		
		//Knappar
		public function NewButton() {
			if (isset($_POST[$this->newButton]) == true) {
				return true;
			}	
			return false;
		}
		
		public function SubmitButton() {
			if (isset($_POST[$this->submit]) == true) {
				return true;
			}	
			return false;
		}
		
		public function NewAnswerButton() {
			if (isset($_POST[$this->newAnswer]) == true) {
				return true;
			}	
			return false;
		}
		
		public function AnswerButton() {
			if (isset($_POST[$this->answerSubmit]) == true) {
				return true;
			}	
			return false;
		}
		
		public function EditButton() {
			if (isset($_POST[$this->editSubmit]) == true) {
				return true;
			}	
			return false;
		}
		
		//Hantera länkar
		public function IsViewingList() {
			
			if (isset($_GET[$this->list])) {
				return true;
			}
			return false;
		}
				
		public function GetSelectedLink() {
			
			if (isset($_GET[$this->id])) {
				return $_GET[$this->id];
			}
			return false;
		}
		
		public function GetDeleteLink() {
			
			if (isset($_GET[$this->del])) {
				return $_GET[$this->del];
			}
			return false;
		}
		
		public function GetDeleteAnswerLink() {
			
			if (isset($_GET[$this->delAnswer])) {
				return $_GET[$this->delAnswer];
			}
			return false;
		}
		
		public function GetEditLink() {
			
			if (isset($_GET[$this->editTopic])) {
				return $_GET[$this->editTopic];
			}
			return false;
		}
		
		
		public function SetTopicLink($id) {			
			return "?".$this->id."=".$id;
		}
				
		public function BackToList() {
			return "?".$this->list;
		}
		
		public function SetDeleteLink($id) {
			return "?".$this->del."=".$id;
		}
		
		public function SetDeleteAnswerLink($id) {
			return "?".$this->delAnswer."=".$id;
		}
		
		public function SetEditLink($id) {
			return "?".$this->editTopic."=".$id;
		}
	}
